package com.dhecp.project.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dhecp.common.utils.IpUtils;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.api.domain.FbaBoxDetail;
import com.dhecp.project.api.mapper.FbaBoxDetailMapper;
import com.dhecp.project.api.service.FbaBoxDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 头程装箱单 服务层
 * @Author fxz
 * @Date 2022/9/23
 */
@Service
public class FbaBoxDetailServicelmpl implements FbaBoxDetailService {

    @Autowired
    private FbaBoxDetailMapper fbaBoxDetailMapper;



    /**
     * 查询头程计划单
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<FbaBoxDetail> queryList(Map<String, Object> params) throws Exception {
        //获取当前装箱单有多少头程订单
        List<FbaBoxDetail> queryList;
        //头程计划单下所有箱子数据
        List<FbaBoxDetail> fbaBoxDetailList = fbaBoxDetailMapper.findFbaBoxDetail(params);

        //fbaBoxDetailList为空新增  反之修改
        List<FbaBoxDetail> fbaBoxDetailListNew = null;
        if (fbaBoxDetailList.size() > 0){
            queryList = new ArrayList<>();
            fbaBoxDetailListNew = JSON.parseArray(fbaBoxDetailList.get(0).getProductInfo(), FbaBoxDetail.class);
            for (int i = 0; i < fbaBoxDetailListNew.size(); i++) {
                queryList.add(fbaBoxDetailListNew.get(i));
            }
        } else {
            queryList = fbaBoxDetailMapper.select(params);
            for (int i = 0; i < queryList.size(); i++) {
                //设置行号
                queryList.get(i).setSerial((i+1)+"");
            }
        }

        FbaBoxDetail fbaBoxDetail;
        int tag = 0;
        //1.装箱 2.发货
        int num = Integer.parseInt(params.get("num").toString());
        if (num == 1){
            tag = 5;
        } else if (num == 2 || num ==3){
            tag = 6;
        }
        String serial = "";
        for (int i = 0; i < tag; i++) {
            fbaBoxDetail = new FbaBoxDetail();
            switch (i) {
                case 0:
                    serial = "箱重(kg)";
                    break;
                case 1:
                    serial = "箱规长(cm)";
                    break;
                case 2:
                    serial = "箱规宽(cm)";
                    break;
                case 3:
                    serial = "箱规高(cm)";
                    break;
                case 4:
                    serial = "抛重(kg)";
                    break;
                case 5:
                    serial = "商品采购价(￡)";
                    break;
            }
            //添加箱重/长宽高等数据
            addJx(fbaBoxDetailList,fbaBoxDetail,i);

            fbaBoxDetail.setScrapData("1");
            fbaBoxDetail.setSerial(serial);
            queryList.add(fbaBoxDetail);
        }
        return queryList;
    }

    /**
     * 查询自动头程计划单
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<FbaBoxDetail> queryAutoList(Map<String, Object> params) throws Exception {
        //获取当前装箱单有多少头程订单
        List<FbaBoxDetail> queryList = null;
        //合并后的头程订单
        List<FbaBoxDetail> queryListZd = new ArrayList<>();
        //头程计划单下所有箱子数据
        List<FbaBoxDetail> fbaBoxDetailList = fbaBoxDetailMapper.findFbaBoxDetail(params);

        //fbaBoxDetailList为空新增  反之修改
        List<FbaBoxDetail> fbaBoxDetailListNew = null;
        String numRealityDeliver;
        Map<String,List<BigDecimal>> boxListMap = new HashMap<>();
        List<BigDecimal> boxList;
        List<BigDecimal> boxListTol;

        int tag = 0;
        int num = Integer.parseInt(params.get("num").toString());
        if (num == 1){
            tag = 5;
        }
        if (fbaBoxDetailList.size() > 0) {
            queryList = new ArrayList<>();
            //取出每条箱子的商品数据将箱数赋值到对应的箱子数据上
            for (int j = 0; j < fbaBoxDetailList.size(); j++) {
                fbaBoxDetailListNew = JSON.parseArray(fbaBoxDetailList.get(j).getProductInfo(), FbaBoxDetail.class);
                for (int i = 0; i < fbaBoxDetailListNew.size(); i++) {
                    boxList = new LinkedList<>();
                    numRealityDeliver = fbaBoxDetailListNew.get(i).getNumRealityDeliver().equals("") ? "0" : fbaBoxDetailListNew.get(i).getNumRealityDeliver();
                    if (j != 0) {
                        //计算商品的计划数量和实际数量
                        queryList.get(i).setNumRealityDeliver(
                                String.valueOf(new Integer(queryList.get(i).getNumRealityDeliver().equals("") ? "0" : queryList.get(i).getNumRealityDeliver())
                                        + new Integer(numRealityDeliver)));
                    }
                    //将每个商品的箱子数据按照数组形式存储起来
                    if (boxListMap.get(fbaBoxDetailListNew.get(i).getSerial()) == null) {
                        boxList.add(new BigDecimal(numRealityDeliver));
                        boxListMap.put(fbaBoxDetailListNew.get(i).getSerial(), boxList);
                    } else {
                        boxList = boxListMap.get(fbaBoxDetailListNew.get(i).getSerial());
                        if (fbaBoxDetailListNew.get(i).getNumRealityDeliver().equals("")) {
                            boxList.add(BigDecimal.ZERO);
                        } else {
                            boxList.add(new BigDecimal(fbaBoxDetailListNew.get(i).getNumRealityDeliver()));
                        }
                        boxListMap.put(fbaBoxDetailListNew.get(i).getSerial(), boxList);
                    }
                    if (j == 0) {
                        fbaBoxDetailListNew.get(i).setBoxList("");
                        queryList.add(fbaBoxDetailListNew.get(i));
                    }
                }

                //箱重-追踪编号的每箱的数据设置进数组中
                String serial = "";
                String val = "";
                for (int i = 1; i < tag; i++) {
                    boxListTol = new LinkedList<>();
                    switch (i) {
                        case 1:
                            boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getWeight()));
                            val = fbaBoxDetailList.get(j).getWeight();
                            serial = "箱重(kg)";
                            break;
                        case 2:
                            serial = "箱规长(cm)";
                            boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getLength()));
                            val = fbaBoxDetailList.get(j).getLength();
                            break;
                        case 3:
                            serial = "箱规宽(cm)";
                            boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getWidth()));
                            val = fbaBoxDetailList.get(j).getWidth();
                            break;
                        case 4:
                            serial = "箱规高(cm)";
                            boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getHeight()));
                            val = fbaBoxDetailList.get(j).getHeight();
                            break;
                        case 5:
                            serial = "抛重(kg)";
                            boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getWeightVol()));
                            val = fbaBoxDetailList.get(j).getWeightVol();
                            break;
                        case 6:
                            serial = "单箱采购总价(￥)";
                            boxListTol.add(fbaBoxDetailList.get(j).getPriceSingleBoxReq());
                            val = String.valueOf(fbaBoxDetailList.get(j).getPriceSingleBoxReq());
                            break;
                        case 7:
                            serial = "追踪编号";
                            if (fbaBoxDetailList.get(j).getTrackingCode() == null){
                                boxListTol.add(BigDecimal.ZERO);
                                val = "0";
                            }else {
                                boxListTol.add(new BigDecimal(fbaBoxDetailList.get(j).getTrackingCode()));
                                val = fbaBoxDetailList.get(j).getTrackingCode();
                            }
                            break;
                    }
                    if (j == 0) {
                        boxListMap.put(serial, boxListTol);
                    } else {
                        boxListTol = boxListMap.get(serial);
                        if (val.equals("")) {
                            boxListTol.add(BigDecimal.ZERO);
                        } else {
                            boxListTol.add(new BigDecimal(val));
                        }
                        boxListMap.put(serial, boxListTol);
                    }
                }
            }

            //进行排序（naturalOrder 正序）（reverseOrder倒叙）
            queryList = queryList.stream().
                    sorted(Comparator.comparing(FbaBoxDetail::getCommodityCode, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaBoxDetail::getCodeStockLocation, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaBoxDetail::getCodeStockArea, Comparator.naturalOrder())).
                    sorted(Comparator.comparing(FbaBoxDetail::getCodeStockOn, Comparator.naturalOrder())).
                    collect(Collectors.toList());

            //将相同的sku、Asin、FnSku相加起来
            Set<String> fnSkuList = new HashSet<>();
            for (FbaBoxDetail fbaBoxDetail : queryList) {
                fbaBoxDetail.setBoxList(boxListMap.get(fbaBoxDetail.getSerial()).toString());
                fnSkuList.add(fbaBoxDetail.getFnsku());
            }
            List<FbaBoxDetail> fbaBoxDetailListOld;
            JSONArray boxListNew;
            List<BigDecimal> list = new ArrayList<>();
            List<BigDecimal> listNew;
            BigDecimal numPlanDeliver;
            BigDecimal numRealityDeliver1;
            for (String fnSku : fnSkuList) {
                fbaBoxDetailListOld = queryList.stream().filter(
                        fbaBoxDetail1 -> fbaBoxDetail1.getFnsku().equals(fnSku)
                ).collect(Collectors.toList());

                numPlanDeliver = BigDecimal.ZERO;
                numRealityDeliver1 = BigDecimal.ZERO;
                for (int i = 0; i < fbaBoxDetailListOld.size(); i++) {
                    boxListNew = JSONArray.parseArray(fbaBoxDetailListOld.get(i).getBoxList());
                    listNew = JSONArray.parseArray(boxListNew.toJSONString(), BigDecimal.class);
                    if (list.size() != 0) {
                        for (int j = 0; j < list.size(); j++) {
                            list.set(j, list.get(j).add(listNew.get(j)));
                        }
                    }
                    if (i == 0) {
                        list = JSONArray.parseArray(boxListNew.toJSONString(), BigDecimal.class);
                    }
                    numPlanDeliver = numPlanDeliver.add(new BigDecimal(fbaBoxDetailListOld.get(i).getNumPlanDeliver()));
                    numRealityDeliver1 = numRealityDeliver1.add(new BigDecimal(fbaBoxDetailListOld.get(i).getNumRealityDeliver()));
                }
                fbaBoxDetailListOld.get(0).setBoxList(list.toString());
                fbaBoxDetailListOld.get(0).setNumPlanDeliver(numPlanDeliver.toString());
                fbaBoxDetailListOld.get(0).setNumRealityDeliver(numRealityDeliver1.toString());
                queryListZd.add(fbaBoxDetailListOld.get(0));
            }
        }

        FbaBoxDetail fbaBoxDetail;
        String serial = "";
        for (int i = 1; i < tag; i++) {
            fbaBoxDetail = new FbaBoxDetail();
            switch (i) {
                case 0:
                    serial = "合计";
                    break;
                case 1:
                    serial = "箱重(kg)";
                    break;
                case 2:
                    serial = "箱规长(cm)";
                    break;
                case 3:
                    serial = "箱规宽(cm)";
                    break;
                case 4:
                    serial = "箱规高(cm)";
                    break;
                case 5:
                    serial = "抛重(kg)";
                    break;
                case 6:
                    serial = "单箱采购总价(￥)";
                    break;
                case 7:
                    serial = "追踪编号";
                    break;
            }
            if (i != 0){
                fbaBoxDetail.setBoxList(boxListMap.get(serial)==null?null:boxListMap.get(serial).toString());
            }
            fbaBoxDetail.setScrapData("1");
            fbaBoxDetail.setSerial(serial);
            queryListZd.add(fbaBoxDetail);
        }
        return queryListZd;
    }


    private void  addJx(List<FbaBoxDetail> fbaBoxDetailList,FbaBoxDetail fbaBoxDetail,Integer type){
        String valueNum = "";
        for (int j = 0; j < fbaBoxDetailList.size(); j++) {
            if (type == 0){
                valueNum = fbaBoxDetailList.get(j).getWeight();
            } else  if (type == 1){
                valueNum = fbaBoxDetailList.get(j).getLength();
            }  else  if (type == 2){
                valueNum = fbaBoxDetailList.get(j).getWidth();
            } else  if (type == 3){
                valueNum = fbaBoxDetailList.get(j).getHeight();
            } else  if (type == 4){
                valueNum = fbaBoxDetailList.get(j).getWeightVol();
            }
            switch (j) {
                case 0:
                    fbaBoxDetail.setTheFirstCase(valueNum);
                    break;
                case 1:
                    fbaBoxDetail.setTheSecondCase(valueNum);
                    break;
                case 2:
                    fbaBoxDetail.setTheThirdCase(valueNum);
                    break;
                case 3:
                    fbaBoxDetail.setTheFourCase(valueNum);
                    break;
                case 4:
                    fbaBoxDetail.setTheFiveCase(valueNum);
                    break;
                case 5:
                    fbaBoxDetail.setTheFixCase(valueNum);
                    break;
                case 6:
                    fbaBoxDetail.setTheSevenCase(valueNum);
                    break;
                case 7:
                    fbaBoxDetail.setTheEightCase(valueNum);
                    break;
                case 8:
                    fbaBoxDetail.setTheNineCase(valueNum);
                    break;
                case 9:
                    fbaBoxDetail.setTheTenCase(valueNum);
                    break;
            }
        }
    }
}
